﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities.Dispatcher;
using Common.Utilities;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ExtractionInstruction:InstructionBase
    {
        #region props
        private int _RunID;
        /// <summary></summary>
        public int RunID
        {
            get
            {
                return this._RunID;
            }
            set
            {
                this._RunID = value;
            }
        }

        private string _Workstation;
        /// <summary></summary>
        public string Workstation
        {
            get
            {
                return this._Workstation;
            }
            set
            {
                this._Workstation = value;
            }
        }

        private string _OperatorName;
        /// <summary></summary>
        public string OperatorName
        {
            get
            {
                return this._OperatorName;
            }
            set
            {
                this._OperatorName = value;
            }
        }

        private int[] _FiscalYears;
        /// <summary></summary>
        public int[] FiscalYears
        {
            get
            {
                return this._FiscalYears;
            }
            set
            {
                this._FiscalYears = value;
            }
        }

        private string[] _ActivityCodes;
        /// <summary></summary>
        public string[] ActivityCodes
        {
            get
            {
                return this._ActivityCodes;
            }
            set
            {
                this._ActivityCodes = value;
            }
        }

        private string[] _ApplTypeCodes;
        /// <summary></summary>
        public string[] ApplTypeCodes
        {
            get
            {
                return this._ApplTypeCodes;
            }
            set
            {
                this._ApplTypeCodes = value;
            }
        }

        private string[] _DocTypeCodes;
        /// <summary></summary>
        public string[] DocTypeCodes
        {
            get
            {
                return this._DocTypeCodes;
            }
            set
            {
                this._DocTypeCodes = value;
            }
        }

        private string _LogFilePath;
        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._LogFilePath;
            }
            set
            {
                this._LogFilePath = value;
            }
        }

        private string _ErrorFilePath;
        /// <summary></summary>
        public string ErrorFilePath
        {
            get
            {
                return this._ErrorFilePath;
            }
            set
            {
                this._ErrorFilePath = value;
            }
        }

        private string _LocalDocRootFolderPath;
        /// <summary></summary>
        public string LocalDocRootFolderPath
        {
            get
            {
                return this._LocalDocRootFolderPath;
            }
            set
            {
                this._LocalDocRootFolderPath = value;
            }
        }
        #endregion

        #region ctor
        ///<summary></summary>
        public ExtractionInstruction()
            : base(Guid.Empty, Guid.Empty)
        {
            this._RunID = 0;
            this._Workstation = Environment.MachineName;
            this._OperatorName = Environment.UserName;
            this._FiscalYears = null;
            this._ActivityCodes = null;
            this._ApplTypeCodes = null;
            this._DocTypeCodes = null;
            this._LogFilePath = string.Empty;
            this._ErrorFilePath = string.Empty;
            this._LocalDocRootFolderPath = string.Empty;
        }

        ///<summary></summary>
        public ExtractionInstruction(
            Guid componentID,
            Guid jobTicketID,
            int runID,
            string workstation,
            string operatorName,
            int[] fys,
            string[] activityCodes,
            string[] applTypeCodes,
            string[] docTypeCodes,
            string logFilePath,
            string errorFilePath, 
            string localDocFolderPath)
            : base(componentID, jobTicketID)
        {
            this._RunID = runID;
            this._Workstation = workstation;
            this._OperatorName = operatorName;
            this._FiscalYears = fys;
            this._ActivityCodes = activityCodes;
            this._ApplTypeCodes = applTypeCodes;
            this._DocTypeCodes = docTypeCodes;
            this._LogFilePath = logFilePath;
            this._ErrorFilePath = errorFilePath;
            this._LocalDocRootFolderPath = localDocFolderPath;
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RunID", this.RunID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Workstation", this.Workstation);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OperatorName", this.OperatorName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FiscalYears", IntUtil.ToString(this.FiscalYears));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ActivityCodes", StringUtil.ToString(this.ActivityCodes));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ApplTypeCodes", StringUtil.ToString(this.ApplTypeCodes));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocTypeCodes", StringUtil.ToString(this.DocTypeCodes));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrorFilePath", this.ErrorFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LocalDocRootFolderPath", this.LocalDocRootFolderPath);
            
            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                ExtractionInstruction instruction = new ExtractionInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.RunID = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "RunID", "0"));
                instruction.Workstation = XmlDataUtil.GetAttributeValue(xNode, "Workstation", "");
                instruction.OperatorName = XmlDataUtil.GetAttributeValue(xNode, "OperatorName", string.Empty);
                instruction.FiscalYears = IntUtil.ToArray(XmlDataUtil.GetAttributeValue(xNode, "FiscalYears", ""));
                instruction.ActivityCodes = StringUtil.Split(XmlDataUtil.GetAttributeValue(xNode, "ActivityCodes", ""));
                instruction.ApplTypeCodes = StringUtil.Split(XmlDataUtil.GetAttributeValue(xNode, "ApplTypeCodes", ""));
                instruction.DocTypeCodes =
                    StringUtil.Split(XmlDataUtil.GetAttributeValue(xNode, "DocTypeCodes", string.Empty));
                instruction.LogFilePath = XmlDataUtil.GetAttributeValue(xNode, "LogFilePath", "");
                instruction.ErrorFilePath = XmlDataUtil.GetAttributeValue(xNode, "ErrorFilePath", "");
                instruction.LocalDocRootFolderPath = XmlDataUtil.GetAttributeValue(xNode, "LocalDocRootFolderPath", "");

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
